package kr.hkit.androidproject_caloriecheck.setting;

import kr.hkit.androidproject_caloriecheck.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class SettingsFragment extends Fragment {
	RadioGroup gender;
	EditText height;
	EditText weight;
	EditText age;
	RadioGroup metabolism;
	Button ok;
	String g = "남자";
	String m = "기본";
	int kcal;
	EditText basic;
	EditText userSetting;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.setting, container, false);

		gender = (RadioGroup) root.findViewById(R.id.gendergroup);
		gender.setOnCheckedChangeListener(mRadioCheck);

		height = (EditText) root.findViewById(R.id.edit_height);
		weight = (EditText) root.findViewById(R.id.edit_weight);
		age = (EditText) root.findViewById(R.id.edit_age);

		metabolism = (RadioGroup) root.findViewById(R.id.metabolismgroup);
		metabolism.setOnCheckedChangeListener(mRadioCheck);

		basic = (EditText) root.findViewById(R.id.edit_basic);
		userSetting = (EditText) root.findViewById(R.id.edit_user_setting);

		ok = (Button) root.findViewById(R.id.btn_ok);
		ok.setOnClickListener(mOnClick);
				
		return root;
	}
	
	public void CalCalorie(){
		if (g.equals("남자")) {
			kcal = (int) (66.47
					+ (13.75 * Integer.parseInt(weight.getText()
							.toString()))
					+ (5 * Integer
							.parseInt(height.getText().toString())) - (6.76 * Integer
					.parseInt(age.getText().toString())));
		} else if (g.equals("여자")) {
			kcal = (int) (655.1
					+ (9.56 * Integer.parseInt(weight.getText()
							.toString()))
					+ (1.85 * Integer.parseInt(height.getText()
							.toString())) - (4.68 * Integer
					.parseInt(age.getText().toString())));
		}

		basic.setText(String.valueOf(kcal));

		if (m.equals("사용자 설정")) {
			kcal = Integer.parseInt(userSetting.getText().toString());
		}
		
		String text = g + " " + height.getText().toString() + " "
				+ weight.getText().toString() + " "
				+ age.getText().toString() + " " + m + " " + kcal;

		Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
	}

	Button.OnClickListener mOnClick = new OnClickListener() {
		public void onClick(View v) {
			try {
				validCheck();
				CalCalorie();
			} catch (Exception e) {
				Toast.makeText(getActivity(), e.getMessage(),
						Toast.LENGTH_SHORT).show();
			}
		}
	};

	RadioGroup.OnCheckedChangeListener mRadioCheck = new OnCheckedChangeListener() {
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			if (group.getId() == R.id.gendergroup) {
				switch (checkedId) {
				case R.id.radiobtn_male:
					g = "남자";
					break;
				case R.id.radiobtn_female:
					g = "여자";
					break;
				}
			} else {
				switch (checkedId) {
				case R.id.radio_basic:
					m = "기본";
					break;
				case R.id.radio_user_setting:
					m = "사용자 설정";
					break;
				}
			}
		}
	};

	protected void validCheck() throws Exception {
		String h = height.getText().toString();
		String w = weight.getText().toString();
		String a = age.getText().toString();

		if (h.equals("")) {
			height.requestFocus();
			throw new Exception("키를 입력해주세요.");
		} else if (w.equals("")) {
			weight.requestFocus();
			throw new Exception("몸무게를 입력해주세요.");
		} else if (a.equals("")) {
			age.requestFocus();
			throw new Exception("나이를 입력해주세요.");
		}
	}
}
